package com.sclibackup.listener;

import java.util.Date;
import java.util.List;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;

import com.sclibackup.model.MypktHdr;
import com.sclibackup.model.TSaveDatetime;
import com.sclibackup.service.IOrderService;
import com.sclibackup.service.ITsaveDatetimeService;
import com.sclibackup.util.scheduledExecutor;


public class BackUpListener implements ApplicationListener<ContextRefreshedEvent>{
	
	@Autowired
	IOrderService orderService;
	@Autowired
	ITsaveDatetimeService tsdService;

	@Override
	public void onApplicationEvent(ContextRefreshedEvent event) {
		ScheduledExecutorService executor = scheduledExecutor.getInstance();
		executor.scheduleAtFixedRate(new Runnable() {

			@Override
			public void run() {
				try{
					TSaveDatetime tsd = tsdService.getPreDate(1);
					Date dd = new Date();
					List<MypktHdr> list = orderService.GetMypktHdrDataByDate(tsd.getPreDate(), dd);
					for(MypktHdr hdr:list){
						orderService.SaveMypktHdr(hdr);
					}
				}catch (Exception e) {
					e.printStackTrace();
				}
			}
			
		}, 0, 5, TimeUnit.MINUTES);
	}

}
